"""
https://leetcode.cn/problems/actors-and-directors-who-cooperated-at-least-three-times/description/?envType=study-plan-v2&envId=30-days-of-pandas&lang=pythondata

1050. 合作过至少三次的演员和导演
简单
相关标签
premium lock icon
相关企业
SQL Schema
Pandas Schema
ActorDirector 表：

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| actor_id    | int     |
| director_id | int     |
| timestamp   | int     |
+-------------+---------+
timestamp 是这张表的主键(具有唯一值的列).
 

编写解决方案找出合作过至少三次的演员和导演的 id 对 (actor_id, director_id)

 

示例 1：

输入：
ActorDirector 表：
+-------------+-------------+-------------+
| actor_id    | director_id | timestamp   |
+-------------+-------------+-------------+
| 1           | 1           | 0           |
| 1           | 1           | 1           |
| 1           | 1           | 2           |
| 1           | 2           | 3           |
| 1           | 2           | 4           |
| 2           | 1           | 5           |
| 2           | 1           | 6           |
+-------------+-------------+-------------+
输出：
+-------------+-------------+
| actor_id    | director_id |
+-------------+-------------+
| 1           | 1           |
+-------------+-------------+
解释：
唯一的 id 对是 (1, 1)，他们恰好合作了 3 次。

"""

import pandas as pd

def actors_and_directors(actor_director: pd.DataFrame) -> pd.DataFrame:
    temp= actor_director.groupby(['actor_id','director_id']).agg({
        'timestamp':'count'
    }).reset_index()
    return temp.loc[temp['timestamp']>=3].loc[:,['actor_id','director_id']]
    pass

if __name__=='__main__':
    actor_director = pd.DataFrame({
        'actor_id': [1, 1, 1, 1, 1, 2, 2],
        'director_id': [1, 1, 1, 2, 2, 1, 1],
        'timestamp': [0, 1, 2, 3, 4, 5, 6]
    })
    print(actors_and_directors(actor_director))